package alex.game.state
{
	import alex.game.playerIO.PlayerIOManager;
	import alex.game.updatable.map.BasicMap;
	import alex.game.updatable.map.MapFactory;
	import alex.game.util.GameUtil;
	
	import com.framework.BasicState;
	import com.framework.GG;
	import com.framework.util.Log;
	import com.framework.util.TimerFormat;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import playerio.Client;
	import playerio.Connection;
	import playerio.PlayerIO;
	import playerio.PlayerIOError;
	
	public class LobbyState extends BasicState
	{
		private var affichage:MovieClip;
		private var isInQueue:Boolean = false;
		private var waitingTime:int;
		
		public function LobbyState()
		{
			
		}
		override public function create():void
		{
			display();
			setListeners();
		}
		override public function destroy():void
		{
			this.affichage.bt_play.removeEventListener(MouseEvent.CLICK, bt_play_clicked);
			super.destroy();
		}
		override public function update():void
		{
			if(isInQueue)
			{
				var wait:String = TimerFormat.formatTime((getTimer()-waitingTime)/1000,TimerFormat.MINUTES);

				this.affichage.txt_currentTime.text = "Current waiting time : "+wait;
			}
		}
		private function display():void
		{
			//this.affichage = new STATE_LOBBY;
			this.addChild(affichage);
		}
		private function setListeners():void
		{
			this.affichage.bt_play.addEventListener(MouseEvent.CLICK, bt_play_clicked);
		}
		private function bt_play_clicked(e:MouseEvent):void
		{
			Log.debug(this,"Button Play Clicked");
			isInQueue = true;
			waitingTime = getTimer();
			this.affichage.bt_play.removeEventListener(MouseEvent.CLICK, bt_play_clicked);
			this.affichage.bt_play.visible = false;
			PlayerIOManager.getInstance().connectToMatchMaking("$MatchMaking-FFA$");
			
			PlayerIOManager.getInstance().addEventListener(PlayerIOManager.PLAYERIO_EVENT_JOINING_GAME_ROOM, joiningRoomHandler);
		}
		private function joiningRoomHandler(e:Event):void
		{
			this.affichage.gotoAndStop("connecting");
			PlayerIOManager.getInstance().removeEventListener(PlayerIOManager.PLAYERIO_EVENT_JOINING_GAME_ROOM, joiningRoomHandler);
			PlayerIOManager.getInstance().addEventListener(PlayerIOManager.PLAYERIO_EVENT_ROOM_JOINED, gameRoomJoinedHandler);
		}
		/**
		 * Récupère les informa
		 * 
		 * */
		private function gameRoomJoinedHandler(e:Event):void
		{
			PlayerIOManager.getInstance().removeEventListener(PlayerIOManager.PLAYERIO_EVENT_ROOM_JOINED, gameRoomJoinedHandler);
			
			//Créé la map
			var map:BasicMap = MapFactory.create(GameUtil.getClassFromProtocole(GameUtil.map));
			Log.debug(this,map+"");
			//Créé la state
			var playState:PlayState = PlayStateFactory.create(GameUtil.getClassFromProtocole(GameUtil.gameType));
			
			//Init la data
			playState.initPlayStateData(GameUtil.players, map);
			
			//Change la state
			GG.switchState(playState);
			
		}

	}
}